package com.vates.vsp.gui.cliente;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import com.vates.vsp.Principal;
import com.vates.vsp.entidades.Cliente;
import com.vates.vsp.exceptions.ClienteNoExistException;
import com.vates.vsp.gui.models.ClienteModel;
import com.vates.vsp.servicios.cliente.ClienteService;

public class VentanaMostrarTodosClientes extends JDialog {
	private static final long serialVersionUID = 1L;

	private ClienteService clienteSrv;
	
	private final JPanel contentPanel = new JPanel();
	private JTable table;
	private JPanel panel;
	private JScrollPane scrollPane;

	public VentanaMostrarTodosClientes() {
		initComponents();
		actualizarTabla();
	}
	
	private void initComponents() {
		setModal(true);
		setTitle("- Listado de Clientes -");
		clienteSrv = (ClienteService) Principal.context.getBean("clienteSrv");
		setBounds(100, 100, 922, 592);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(Color.WHITE);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setLocationRelativeTo(null);
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			panel = new JPanel();
			panel.setBorder(new TitledBorder(null, "Todos los clientes", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panel.setBackground(Color.WHITE);
			
			scrollPane = new JScrollPane();
			scrollPane.setViewportBorder(null);
		}
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(5)
					.addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGap(5))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(6)
					.addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGap(6))
		);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		table = new JTable();
		
		scrollPane.setViewportView(table);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setBorder(null);
		panel.setLayout(gl_panel);

		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(Color.WHITE);
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton btnAceptar = new JButton("Cerrar");
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				
				JButton btnNuevo = new JButton("Nuevo...");
				btnNuevo.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						nuevoCliente();
					}
				});
				buttonPane.add(btnNuevo);
				
				JButton btnEliminarSeleccin = new JButton("Eliminar selecci\u00F3n");
				btnEliminarSeleccin.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						eliminarSeleccion();
					}
				});
				
				JButton btnBuscar = new JButton("Buscar...");
				btnBuscar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						buscar();
					}
				});
				buttonPane.add(btnBuscar);
				
				JButton btnEditarSeleccion = new JButton("Editar selecci\u00F3n...");
				btnEditarSeleccion.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						editarSeleccion();
					}
				});
				buttonPane.add(btnEditarSeleccion);
				buttonPane.add(btnEliminarSeleccin);
				btnAceptar.setActionCommand("OK");
				buttonPane.add(btnAceptar);
				getRootPane().setDefaultButton(btnAceptar);
			}
		}
	}
	
	private void nuevoCliente() {
		JDialog jd = new VentanaClienteNuevo();
		jd.setVisible(true);
		actualizarTabla();
	}
	
	private void actualizarTabla() {
		table.setModel(new ClienteModel(clienteSrv.buscarTodosClientes()));
	}
	
	private void eliminarSeleccion() {
		try {
			if(this.table.getSelectedRowCount() != 0) {
				// Long id = (Long) table.getValueAt(table.getSelectedRow(), 0);
				// Cliente cliente = clienteSrv.buscarClientePorId(id);
				Cliente cliente = obtenerClienteSeleccionado();
				if(JOptionPane.showConfirmDialog(this, "Desea eliminar a " + cliente.getNombre() + "?", "Eliminar Cliente", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0) {
				clienteSrv.bajaCliente(cliente);
				actualizarTabla();}
			}
		} catch (ClienteNoExistException e) { }
	}
	
	private Cliente obtenerClienteSeleccionado() {
		try {
			Long id = (Long) table.getValueAt(table.getSelectedRow(), 0);
			return clienteSrv.buscarClientePorId(id);
			} catch (ClienteNoExistException e) { }
		return null;
	}
	
	private void buscar() {
		JDialog jd = new VentanaEditarCliente();
		jd.setVisible(true);
		actualizarTabla();
	}
	
	private void editarSeleccion() {
		if(this.table.getSelectedRowCount() != 0) {
			JDialog jd = new VentanaEditarCliente(obtenerClienteSeleccionado());
			jd.setVisible(true);
			actualizarTabla();
		}
	}
}
